package com.cn.sl.server.util

import com.cn.sl.server.common.constant.Constant
import com.cn.sl.server.common.constant.MessageConstant
import io.swagger.v3.oas.annotations.media.Schema
import org.slf4j.MDC
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity

/**
 * 公共相应工具类
 *
 * @author 阮雪峰
 */
/**
 * 成功响应
 *
 * @param body 响应数据
 * @param <T>  响应数据
 * @return ResponseEntity<T>
</T></T> */
fun <T> ok(body: T): ResponseEntity<T> {
    return ResponseEntity.ok(body)
}

/**
 * 响应成功消息
 */
fun requestSuccessMsg(): ResponseEntity<ResponseMessageDto> {
    return ok(MessageConstant.SUCCESS)
}

fun saveSuccessMsg(): ResponseEntity<ResponseMessageDto> {
    return ok(MessageConstant.SAVE_SUCCESS)
}

fun deleteSuccessMsg(): ResponseEntity<ResponseMessageDto> {
    return ok(MessageConstant.DELETE_SUCCESS)
}

/**
 * 成功响应
 *
 * @param body 响应消息
 * @return ResponseEntity<ResponseMessageDto>
</ResponseMessageDto> */
fun ok(body: String?): ResponseEntity<ResponseMessageDto> {
    return ResponseEntity.ok(ResponseMessageDto(body))
}

fun okStrRes(body: String): ResponseEntity<String> {
    return ResponseEntity.ok(body)
}

/**
 * bad request 响应
 *
 * @param body 响应数据
 * @return ResponseEntity<ResponseMessageDto>
</ResponseMessageDto> */
fun badRequest(body: String?): ResponseEntity<ResponseMessageDto> {
    return ResponseEntity.badRequest().body(ResponseMessageDto(body))
}

/**
 * 未授权
 *
 * @param body body
 * @return ResponseEntity<ResponseMessageDto>
</ResponseMessageDto> */
fun unauthorized(body: String?): ResponseEntity<ResponseMessageDto> {
    return ResponseEntity<ResponseMessageDto>(ResponseMessageDto(body), HttpStatus.UNAUTHORIZED)
}

/**
 * @param body body
 * @return ResponseEntity<ResponseMessageDto>
</ResponseMessageDto> */
fun notAcceptable(body: String?): ResponseEntity<ResponseMessageDto> {
    return ResponseEntity<ResponseMessageDto>(ResponseMessageDto(body), HttpStatus.NOT_ACCEPTABLE)
}

/**
 * @param body body
 * @return ResponseEntity<ResponseMessageDto>
</ResponseMessageDto> */
fun preconditionFailed(body: String?): ResponseEntity<ResponseMessageDto> {
    return ResponseEntity<ResponseMessageDto>(ResponseMessageDto(body), HttpStatus.PRECONDITION_FAILED)
}

/**
 * @param body body
 * @return ResponseEntity<ResponseMessageDto>
</ResponseMessageDto> */
fun forbidden(body: String?): ResponseEntity<ResponseMessageDto> {
    return ResponseEntity<ResponseMessageDto>(ResponseMessageDto(body), HttpStatus.FORBIDDEN)
}

/**
 * @param body body
 * @return ResponseEntity<ResponseMessageDto>
</ResponseMessageDto> */
fun serverError(body: String?): ResponseEntity<ResponseMessageDto> {
    return ResponseEntity<ResponseMessageDto>(ResponseMessageDto(body), HttpStatus.INTERNAL_SERVER_ERROR)
}

fun extendResponse(code: HttpStatus, body: String?): ResponseEntity<ResponseMessageDto> {
    return ResponseEntity.status(code).body(ResponseMessageDto(body))
}

fun extendResponse(code: Int, body: String?): ResponseEntity<ResponseMessageDto> {
    return ResponseEntity.status(code).body(ResponseMessageDto(body))
}

fun extendResponse(code: Int, body: Map<String, Any>): ResponseEntity<Map<String, Any>> {
    return ResponseEntity.status(code).body(body)
}

data class ResponseMessageDto(
    /**
     * 消息内容
     */
    @field:Schema(description = "消息内容")
    var message: String? = null,
    /**
     * 请求唯一标识
     */
    @field:Schema(description = "请求唯一标识")
    val traceId: String = MDC.get(Constant.TRACE_ID)
)